/*
 *  Author: Honrun
 */
#ifndef _Micro_TIMER_H_
#define _Micro_TIMER_H_


#include "stdint.h"
#include "microList.h"





typedef enum {
    MicroTimerClose = 0x00,    /* 定时器已关闭 */
    MicroTimerOpen = 0x01,     /* 正常计时当中 */
    MicroTimerOver = 0x02,     /* 定时器已溢出 */
    MicroTimerError = 0x08,    /* 定时器错误 */
    MicroTimerCircular = 0x10, /* 循环计时 */
}MicroTimerStateEnum;



typedef struct
{
    int64_t timeStop;                   /* 定时结束时刻（us） */
    int64_t timeDuration;               /* 定时时长（us） */
    MicroTimerStateEnum state;          /* 状态 */

    MicroListType typeList;             /* 链表 */
    void (*vCallbackFunction)(void *);  /* 回调函数 */
}MicroTimerTypeDef;






int64_t lMicroTimerGetNow(void);


/* 基础定时 功能 */
#define cMicroTimerSetUs(ptypeTimer, lTime, state) cMicroTimerSet((ptypeTimer), (lTime), (state))
#define cMicroTimerSetMs(ptypeTimer, lTime, state) cMicroTimerSet((ptypeTimer), (lTime) * 1000.0f, (state))
#define cMicroTimerSetS(ptypeTimer, lTime, state) cMicroTimerSet((ptypeTimer), (lTime) * 1000000.0f, (state))
int8_t cMicroTimerSet(MicroTimerTypeDef *ptypeTimer, int64_t lTime, MicroTimerStateEnum state);
int8_t cMicroTimerReset(MicroTimerTypeDef *ptypeTimer);
int8_t cMicroTimerReload(MicroTimerTypeDef *ptypeTimer);
int8_t cMicroTimerOpen(MicroTimerTypeDef *ptypeTimer);
int8_t cMicroTimerClose(MicroTimerTypeDef *ptypeTimer);
int8_t cMicroTimerSetState(MicroTimerTypeDef *ptypeTimer, MicroTimerStateEnum enumState);
MicroTimerStateEnum enumMicroTimerGetState(MicroTimerTypeDef *ptypeTimer);


/* 定时调用执行 功能 */
#define cMicroTimerCreateUs(ptypeTimer, lTime, state, vCallbackFunction) cMicroTimerCreate((ptypeTimer), (lTime), (state), (vCallbackFunction))
#define cMicroTimerCreateMs(ptypeTimer, lTime, state, vCallbackFunction) cMicroTimerCreate((ptypeTimer), (lTime) * 1000.0f, (state), (vCallbackFunction))
#define cMicroTimerCreateS(ptypeTimer, lTime, state, vCallbackFunction) cMicroTimerCreate((ptypeTimer), (lTime) * 1000000.0f, (state), (vCallbackFunction))
int8_t cMicroTimerCreate(MicroTimerTypeDef *ptypeTimer, int64_t lTime, MicroTimerStateEnum state, void (*vCallbackFunction)(void *pvHandle));
int8_t cMicroTimerMachine(void);

#endif // DEVICESMicroTIMER_H_INCLUDED
